//
//  DetailViewController.swift
//  通讯录
//
//  Created by lu-pc on 16/12/12.
//  Copyright © 2016年 lu-pc. All rights reserved.
//

import UIKit

class DetailViewController: UITableViewController {
    
    @IBOutlet weak var titleText: UITextField!
    @IBOutlet weak var phoneText: UITextField!
    @IBOutlet weak var nameText: UITextField!
    var person: Person?
    
    // 完成回调属性
    var completionCallBack:(()->())?

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 判断person是否有值，如果有设置UI
        if person != nil {
            nameText.text  = person?.name
            phoneText.text = person?.phone
            titleText.text = person?.title
        }

    }

    
    @IBAction func savePerson(_ sender: UIBarButtonItem) {
        
        // 判断person是否为nil，如果是就新建
        if person == nil {
            person = Person()
        }
        
        // 用UI更新person的内容
        person?.name = nameText.text
        person?.phone = phoneText.text
        person?.title = titleText.text
        
        // 执行闭包回调
        completionCallBack?()
        
        // 返回上一级界面
        _ = navigationController?.popViewController(animated: true)
        
    }

}
